package com.amazon.web.controller;

import com.amazon.bo.KeyWords;
import com.amazon.service.I18nService;
import com.amazon.service.KeyWordsService;
import com.amazon.web.util.ApiListResponse;
import com.amazon.web.util.ApiResponse;
import com.amazon.web.util.OSSManageUtil;
import com.amazon.web.util.ResponseUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value="keywords")
public class KeyWordsController {
    @Value("${is.open.cms}")
    public String isOpenCms;


    private static final String APP_ID = "20180715000185622";
    private static final String SECURITY_KEY = "RtGMlsmDQUDfmQ6Ddi5L";

    @Resource
    private OSSManageUtil ossManageUtil;

    @Resource
    KeyWordsService keyWordsService;

    @Autowired
    I18nService i18nService;

    @RequestMapping(value = "keyWordSave", method = RequestMethod.POST)
    public Object keyWordSave(KeyWords k){
        KeyWords old=keyWordsService.getKeyWordsByCh(k.getChkey());
        if(old!=null){
            old.setDekey(k.getDekey());
            old.setChkey(k.getChkey());
            old.setEskey(k.getEskey());
            old.setEnkey(k.getEnkey());
            old.setFrkey(k.getFrkey());
            old.setJpkey(k.getJpkey());
            old.setItkey(k.getItkey());
            boolean flag =keyWordsService.update(old);
            return flag;
        }else{
            boolean flag=keyWordsService.save(k);
            return flag;
        }
    }
    @RequestMapping(value = "getKeyWordsList", method = RequestMethod.GET)
    public ApiResponse getKeyWordsList(int page, int pagesize){
        Object list=keyWordsService.getKeyWordsList(page,pagesize);
        int total = keyWordsService.getKeyWordsCount();
        return ResponseUtil.getSuccessResponse(list, total);
    }
    @RequestMapping(value = "keyWordDelete", method = RequestMethod.POST)
    public Object keyWordDelete(String words){
        String[] w=words.split(",");
        try {
            for (String s : w) {
                KeyWords k=keyWordsService.getKeyWordsById(Integer.parseInt(s));
                if(k!=null){
                    keyWordsService.keyWordsDelete(k);
                }
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }


}
